package com.shop.cereshop.admin.controller;

import com.shop.cereshop.admin.annotation.NoRepeatSubmit;
import com.shop.cereshop.admin.annotation.NoRepeatWebLog;
import com.shop.cereshop.admin.param.config.ConfigIdParam;
import com.shop.cereshop.admin.param.config.ConfigKeyParam;
import com.shop.cereshop.admin.param.config.ConfigQueryParam;
import com.shop.cereshop.admin.param.config.ConfigSaveParam;
import com.shop.cereshop.admin.param.config.ConfigUpdateParam;
import com.shop.cereshop.admin.service.config.CereConfigService;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.config.CereConfig;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 参数配置管理
 */
@RestController
@RequestMapping("config")
@Slf4j(topic = "ConfigController")
@Api(value = "参数配置模块", tags = "参数配置模块")
public class ConfigController {

    @Autowired
    private CereConfigService cereConfigService;

    /**
     * 新增参数
     */
    @PostMapping("save")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "新增参数")
    public Result<Void> save(@RequestBody @Validated ConfigSaveParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        cereConfigService.save(param, user);
        return Result.success();
    }

    /**
     * 修改参数
     */
    @PostMapping("update")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "修改参数")
    public Result<Void> update(@RequestBody @Validated ConfigUpdateParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        cereConfigService.update(param, user);
        return Result.success();
    }

    /**
     * 删除参数
     */
    @PostMapping("delete")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "删除参数")
    public Result<Void> delete(@RequestBody @Validated ConfigIdParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformUser user = (CerePlatformUser) request.getAttribute("user");
        cereConfigService.delete(param, user);
        return Result.success();
    }

    /**
     * 参数详情
     */
    @PostMapping("getById")
    @ApiOperation(value = "参数详情")
    public Result<CereConfig> getById(@RequestBody @Validated ConfigIdParam param) throws CoBusinessException {
        CereConfig data = cereConfigService.getById(param.getConfigId());
        return Result.success(data);
    }

    /**
     * 参数列表
     */
    @PostMapping("getAll")
    @ApiOperation(value = "参数列表")
    public Result<Page<CereConfig>> getAll(@RequestBody ConfigQueryParam param) throws CoBusinessException {
        Page<CereConfig> page = cereConfigService.getAll(param);
        return Result.success(page);
    }

    /**
     * 根据参数键名获取值（提供给前端和其他端使用）
     */
    @PostMapping("getValue")
    @ApiOperation(value = "根据参数键名获取值")
    public Result<String> getValue(@RequestBody @Validated ConfigKeyParam param) {
        String value = com.shop.cereshop.commons.service.config.SysConfigHelper.getConfigValue(param.getConfigKey());
        return Result.success(value);
    }
}


